package ulmermark.demo.xslt;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;

public class XMLUtil {

	public static void main(String[] args){
		
		if ( args.length != 2) System.exit(0);
		String xmlName = args[0];
		String xsltName = args[1];
		
		try{
		
			Document transformed = transform( string2Document( file2String( xmlName)), string2Document( file2String( xsltName)));
			
			System.out.println(DOM2String(transformed));
		}catch( Exception e)
		{
			e.printStackTrace();
		}
		
		
	}
	 public static Document transform(final Document srcDoc, final Document xslDoc)
		        throws Exception
		    {
		        Document doc = null;
		        DOMSource xslDomSource = new DOMSource(xslDoc);
		        TransformerFactory tFactory = TransformerFactory.newInstance();
		        Transformer transformer = tFactory.newTransformer(xslDomSource);
		        DOMResult domResult = new DOMResult();
		        DOMSource xmlDomSource = new DOMSource(srcDoc);
		        transformer.transform(xmlDomSource, domResult);
		        doc = (Document)domResult.getNode();
		        return doc;
		    }
	 
	    
	    public static String file2String(String fname)
	            throws Exception
	        {
	            BufferedReader br = null;
	            StringBuffer content = new StringBuffer();
	            try
	            {
	                br = new BufferedReader(new FileReader(fname));
	                String thisLine;
	                while((thisLine = br.readLine()) != null) 
	                {
//	                    System.out.println(thisLine);
	                    content.append((new StringBuilder()).append(thisLine).append("\n").toString());
	                }
	                br.close();
	            }
	            catch(IOException e)
	            {
	                System.out.println((new StringBuilder()).append("error: ").append(e.getMessage()).toString());
	                if(br != null)
	                    br.close();
	            }
	            return content.toString();
	        }	    
	    
	    public static Document string2Document( final String xml) throws Exception{
	    	String xmlString; 
	    	DocumentBuilderFactory factory =
	    	  DocumentBuilderFactory.newInstance();

	    	DocumentBuilder builder = factory.newDocumentBuilder();

	    	final Document document =
	    	 builder.parse(new InputSource(new StringReader(xml)));
	    	
//	    	System.out.println(DOM2String( document));
	    	return document;
	    }
	    
	    public static String DOM2String(Document document)
	            throws Exception
	        {
	            String result = null;
	            if(document != null)
	            {
	                StringWriter strWtr = new StringWriter();
	                StreamResult strResult = new StreamResult(strWtr);
	                TransformerFactory tfac = TransformerFactory.newInstance();
	                Transformer t = tfac.newTransformer();
	                t.setOutputProperty("indent", "yes");
	                t.setOutputProperty("method", "xml");
	                t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
	                t.transform(new DOMSource(document.getDocumentElement()), strResult);
	                result = strResult.getWriter().toString();
	            }
	            return result;
	        }
	    
	    /** 
	     * Simple transformation method. 
	     * @param sourcePath - Absolute path to source xml file. 
	     * @param xsltPath - Absolute path to xslt file. 
	     * @param resultDir - Directory where you want to put resulting files. 
	     */  
	    public static void simpleTransform(String sourcePath, String xsltPath,  
	                                       String resultDir) {  
	        TransformerFactory tFactory = TransformerFactory.newInstance();  
	        try {  
	            Transformer transformer =  
	                tFactory.newTransformer(new StreamSource(new File(xsltPath)));  
	  
	            transformer.transform(new StreamSource(new File(sourcePath)),  
	                                  new StreamResult(new File(resultDir)));  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }  
	    }  
}
